/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmVectores.java
 *
 * Created on 03-12-2014, 10:33:20 AM
 */
package ejerciciodeconjuntos;

/**
 *
 * @author Estudiante
 */
import Negocio.Conjuntos;
import Negocio.Entero;
import javax.swing.JOptionPane;
import javax.swing.JTable;

public class frmVectores extends javax.swing.JFrame {

    /** 
     * Crea el nuevo formulario frmVectores 
     */
    public frmVectores() {
        initComponents();
    }    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroup = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla1 = new javax.swing.JTable();
        btnDim = new javax.swing.JButton();
        txtn = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabla2 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabla0 = new javax.swing.JTable();
        txtn1 = new javax.swing.JTextField();
        btnDim1 = new javax.swing.JButton();
        btnDim2 = new javax.swing.JButton();
        rbUnion = new javax.swing.JRadioButton();
        rbInter = new javax.swing.JRadioButton();
        rbDif = new javax.swing.JRadioButton();
        rbDifSim = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabla1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tabla1);

        btnDim.setText("Dimencionar");
        btnDim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDimActionPerformed(evt);
            }
        });

        tabla2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tabla2);

        tabla0.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane3.setViewportView(tabla0);

        btnDim1.setText("Dimencionar");
        btnDim1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDim1ActionPerformed(evt);
            }
        });

        btnDim2.setText("Calcular");
        btnDim2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDim2ActionPerformed(evt);
            }
        });

        btnGroup.add(rbUnion);
        rbUnion.setText("Union");

        btnGroup.add(rbInter);
        rbInter.setText("Intersepcion");

        btnGroup.add(rbDif);
        rbDif.setText("Diferencia");

        btnGroup.add(rbDifSim);
        rbDifSim.setText("Diferencia Simetrica");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Ejercicio de Conjuntos");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtn1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnDim1))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 524, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtn, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnDim))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 524, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(229, 229, 229)
                                .addComponent(btnDim2))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addComponent(rbUnion)
                        .addGap(18, 18, 18)
                        .addComponent(rbInter)
                        .addGap(18, 18, 18)
                        .addComponent(rbDif)
                        .addGap(18, 18, 18)
                        .addComponent(rbDifSim))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(26, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 576, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDim))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtn1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDim1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbUnion)
                    .addComponent(rbInter)
                    .addComponent(rbDif)
                    .addComponent(rbDifSim))
                .addGap(15, 15, 15)
                .addComponent(btnDim2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
        
    private void btnDimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDimActionPerformed
        dimencionar(tabla0,Integer.parseInt(txtn.getText()));        
    }//GEN-LAST:event_btnDimActionPerformed

    private void btnDim1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDim1ActionPerformed
        dimencionar(tabla1,Integer.parseInt(txtn1.getText()));
    }//GEN-LAST:event_btnDim1ActionPerformed
    
    /**
     * Funcion para dimencionar las tablas con su cantidad de columnas para el insertado
     */
    void dimencionar(JTable tab,int d){
        String[] title = new String[d];
        Object[][] obj = new Object[1][d];
        tab.setModel(new javax.swing.table.DefaultTableModel(obj,title));
    }
    
    
    
    private void btnDim2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDim2ActionPerformed
    /*Cracion de los tres conjuntos*/
        Conjuntos   A = new Conjuntos(),
                    B = new Conjuntos(),
                    C = new Conjuntos();
    /**Si algun radioButton esta seleccionado
     * Se instancian los conjuntos A & B y se mandan a cargar con los datos
     * que tienen las tablas respectivas y realizan sus operaciones*/ 
        if(rbUnion.isSelected()){ 
            /*Se cargan los vectores*/
                A.setDim(Integer.parseInt(txtn.getText()));     A.vector=cargar(tabla0,A.getDim());
                B.setDim(Integer.parseInt(txtn1.getText()));    B.vector=cargar(tabla1,B.getDim());
            /*Operacion de la union*/
                C=C.union(A, B);
            /*Se manda a mostrar la union*/
                mostrar(tabla2,C.vector);
        }else if(rbInter.isSelected()){
            /*Se cargan los vectores*/
                A.setDim(Integer.parseInt(txtn.getText()));     A.vector=cargar(tabla0,A.getDim());            
                B.setDim(Integer.parseInt(txtn1.getText()));    B.vector=cargar(tabla1,B.getDim());            
            /*Operacion de la Intersepcion*/
                C=C.Intersepcion(A, B);
            /*Se manda a mostrar la intersepcion*/    
                mostrar(tabla2,C.vector);
        }else if(rbDif.isSelected()){
            /*Se cargan los vectores*/
                A.setDim(Integer.parseInt(txtn.getText()));     A.vector=cargar(tabla0,A.getDim());            
                B.setDim(Integer.parseInt(txtn1.getText()));    B.vector=cargar(tabla1,B.getDim());            
            /*Operacion de la Diferencia*/
                C=C.Diferencia(A, B);
            /*Se manda a mostrar la Diferencia*/    
                mostrar(tabla2,C.vector);
        }else if(rbDifSim.isSelected()){
            /*Se cargan los vectores*/
                A.setDim(Integer.parseInt(txtn.getText()));     A.vector=cargar(tabla0,A.getDim());            
                B.setDim(Integer.parseInt(txtn1.getText()));    B.vector=cargar(tabla1,B.getDim());
                
                Conjuntos Aaux = new Conjuntos(),Baux= new Conjuntos();
                Aaux = Aaux.Diferencia(A, B);
                Baux = Baux.Diferencia(B, A);
                
            /*Operacion de la Diferencia simetrica*/
                C=C.union(Aaux, Baux);
            /*Se manda a mostrar la Diferencia simetrica*/    
                mostrar(tabla2,C.vector);
        }
    }//GEN-LAST:event_btnDim2ActionPerformed
    
    
    
    /**
     * Funcion que devuelve un vector de tipo Entero que son los elementos de un conjunto
     */
    public Entero[] cargar(JTable tb,int d){
            Entero[] aux= new Entero[d];
            for(int i=0;i<tb.getColumnCount();i++)
                aux[i] = new Entero(Integer.parseInt(tb.getValueAt(0, i).toString()));        
            return aux;
        }
    /**
     * Procedimientos sobrecargados para el mostrado de los conjuntos primero se debe
     * invocar al mostrar #1 y el redirecciona al mostrar #2
     */
    public void mostrar/*#1*/(JTable tb,Entero[] vec){
        int i=0;
        while(i<vec.length && vec[i]!= null)
            i++;
        mostrar(tb,vec,i);
    }    
    public void mostrar/*#2*/(JTable tb, Entero[] v,int d){
        dimencionar(tb,d);
        int i=0;
        while(i<d){
            tb.setValueAt(v[i].getNum(), 0, i);
            i++;
        }
    }
    
    
    
    /**
     * @param args the command line arguments
     */    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmVectores().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDim;
    private javax.swing.JButton btnDim1;
    private javax.swing.JButton btnDim2;
    private javax.swing.ButtonGroup btnGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JRadioButton rbDif;
    private javax.swing.JRadioButton rbDifSim;
    private javax.swing.JRadioButton rbInter;
    private javax.swing.JRadioButton rbUnion;
    private javax.swing.JTable tabla0;
    private javax.swing.JTable tabla1;
    private javax.swing.JTable tabla2;
    private javax.swing.JTextField txtn;
    private javax.swing.JTextField txtn1;
    // End of variables declaration//GEN-END:variables
}
